#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os.path
import re
import tornado.database
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import base64
import hashlib

from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
define("mysql_host", default="localhost:3306", help="blog database host")
define("mysql_database", default="chihuo", help="blog database name")
define("mysql_user", default="root", help="blog database user")
define("mysql_password", default="", help="blog database password")

import admin
import business
import auth
import front
    
class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/(order[a-z/]*)?",front.OrderHandler),
            (r"/show/(item|chef)/(\d+)",front.ShowHandler),
            (r"/(login|logout)",auth.AuthHandler),
            (r"/admin/(.+)?",admin.AdminHandler),
            (r"/business/(.+)?",business.BusinessHandler),
        ]
        settings = dict(
            site_title=u"ChiHuo",
            template_path=os.path.join(os.path.dirname(__file__),"templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
            ui_modules={"Entry": EntryModule},
            debug=True,
        )
        tornado.web.Application.__init__(self,handlers,**settings)

        # database
        self.db = tornado.database.Connection(
            host=options.mysql_host,database=options.mysql_database,
            user=options.mysql_user,password=options.mysql_password)

# UI Module
class EntryModule(tornado.web.UIModule):
    def todo(self):
        pass

def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
